package com.example.neteasecloudmusic.ui.song_list_detail;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.neteasecloudmusic.R;
import com.example.neteasecloudmusic.bean.playListBean.Tracks;
import com.example.neteasecloudmusic.ui.PlayListActivity;

import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {

    private RecyclerView recyclerView;
    private LayoutInflater layoutInflater;
    private Context context;
    private List<Tracks> tracks;
    private OnItemClickListener listener;


    public PlayListAdapter(RecyclerView recyclerView, PlayListActivity context, List<Tracks> tracks) {
        this.recyclerView = recyclerView;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.tracks = tracks;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(viewType, parent, false);
        view.setOnClickListener(this);
        if (viewType == R.layout.playlist_recycleview)
            return new PlayListViewHolder(view);
        return new SingleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Tracks tracks = this.tracks.get(position);
        int itemViewType = getItemViewType(position);
        PlayListViewHolder playListViewHolder = (PlayListViewHolder) holder;
        ((PlayListViewHolder) holder).serial_number.setText(String.valueOf(position + 1));
        ((PlayListViewHolder) holder).song_title.setText(tracks.getName());
        ((PlayListViewHolder) holder).song_singer.setText(tracks.getAr().get(0).getName());
    }

    @Override
    public int getItemViewType(int position) {
        return R.layout.playlist_recycleview;
    }

    @Override
    public int getItemCount() {
        return tracks.size();
    }

    public void setData(List<Tracks> tracks) {
        this.tracks = tracks;
        notifyDataSetChanged();
    }

    private class PlayListViewHolder extends RecyclerView.ViewHolder {

        TextView serial_number, song_title, song_singer;

        public PlayListViewHolder(View view) {
            super(view);
            serial_number = view.findViewById(R.id.serial_number);
            song_title = view.findViewById(R.id.song_title);
            song_singer = view.findViewById(R.id.song_singer);
        }
    }

    class SingleViewHolder extends RecyclerView.ViewHolder {

        public SingleViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    @Override
    public void onClick(View v) {
        if (listener != null) {
            int position = recyclerView.getChildAdapterPosition(v);
            listener.onItemClick(position);
        }
    }

    public void setOnItemClickListener (OnItemClickListener listener) {
        this.listener = listener;
    }


    public interface OnItemClickListener {
        void onItemClick(int position);
    }
}



























